// 转化货币
export function moneyFilter(num) {
  var moneyNum = +num;
  if (!num) {
    moneyNum = 0;
    return moneyNum.toFixed(2);
  } else if (isNaN(moneyNum)) {
    return "-";
  } else {
    return moneyNum.toFixed(2);
  }
}
//超出省略号
export function ellipsis (value) {
  if (!value) return ''
  if (value.length > 8) {
    return value.slice(0,8) + '...'
  }
  return value
}
// 转化整数
export function parseIntFilter(num) {
  var moneyNum = +num;
  if (!num) {
    moneyNum = 0;
    return parseInt(moneyNum);
  } else if (isNaN(moneyNum)) {
    return "-";
  } else {
    return parseInt(moneyNum);
  }
}
// 转化时间戳
export function timeStampFilter(time, cFormat) {
  if (arguments.length === 0) {
    return null;
  }
  var format = cFormat || "{y}/{m}/{d} ";
  var date;
  if (typeof time === "object") {
    date = time;
  } else {
    if (("" + time).length === 10) time = parseInt(time) * 1000;
    date = new Date(time);
  }
  var formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    a: date.getDay()
  };
  var time_str = format.replace(/{(y|m|d|a)+}/g, (result, key) => {
    var value = formatObj[key];
    if (key === "a")
      return ["一", "二", "三", "四", "五", "六", "日"][value - 1];
    if (result.length > 0 && value < 10) {
      value = "0" + value;
    }
    return value || 0;
  });
  return time_str;
}
// 转化时间戳
export function timeFilter(time, cFormat) {
  if (arguments.length === 0) {
    return null;
  }
  var format = cFormat || "{y}-{m}-{d} {h}:{i}:{s}";
  var date;
  if (typeof time === "object") {
    date = time;
  } else {
    if (("" + time).length === 10) time = parseInt(time) * 1000;
    date = new Date(time);
  }
  var formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  };
  var time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    var value = formatObj[key];
    if (key === "a")
      return ["一", "二", "三", "四", "五", "六", "日"][value - 1];
    if (result.length > 0 && value < 10) {
      value = "0" + value;
    }
    return value || 0;
  });
  return time_str;
}